package ch.pizzapp.kurier.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import ch.pizzapp.kurier.Constants;
import ch.pizzapp.kurier.R;
import ch.pizzapp.kurier.data.GenarateItem;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

public class GridNormalAdapater extends BaseAdapter {
	List<GenarateItem> genarateItemList;
	private final DisplayImageOptions options;
	private final ImageLoader imageLoader;
	Context mContext;
	LayoutInflater mInflater;
	SparseBooleanArray mSparseBooleanArray;

	List<GenarateItem> selectedGenarateItemList;

	public GridNormalAdapater(Context context,
			List<GenarateItem> genarateItemList) {
		super();
		mContext = context;
		if (mContext != null) {
			mInflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		this.genarateItemList = genarateItemList;
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.white_background)
				.showImageForEmptyUri(R.drawable.white_background)
				.showImageOnFail(R.drawable.white_background).cacheOnDisc(true)
				.displayer(new SimpleBitmapDisplayer())
				.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
				.bitmapConfig(Bitmap.Config.RGB_565).build(); // default;
		imageLoader = ImageLoader.getInstance();

		mSparseBooleanArray = new SparseBooleanArray();
	}

	public ArrayList<GenarateItem> getCheckedItems() {
		ArrayList<GenarateItem> mTempArry = new ArrayList<GenarateItem>();

		for (int i = 0; i < genarateItemList.size(); i++) {
			if (mSparseBooleanArray.get(i)) {
				mTempArry.add(genarateItemList.get(i));
			}
		}

		return mTempArry;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (genarateItemList != null) {
			size = genarateItemList.size();
		}

		return size;

	}

	@Override
	public GenarateItem getItem(int position) {
		return genarateItemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return Long.valueOf(genarateItemList.get(position).getItemID());
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;

		holder = new ViewHolder();
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.generator_grid_item, null);

			holder.textViewTitle = (TextView) convertView
					.findViewById(R.id.textViewTitle);
			holder.textViewDescription = (TextView) convertView
					.findViewById(R.id.textViewDescription);
			holder.textViewPrice = (TextView) convertView
					.findViewById(R.id.textViewPrice);
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.imageView);
			holder.checkbox = (CheckBox) convertView
					.findViewById(R.id.radioButton);

			convertView.setTag(holder);
		}

		else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.checkbox.setTag(position);

		// set values
		holder.textViewTitle.setText(getItem(position).getTitle());
		holder.textViewDescription.setText(getItem(position).getDesc());
		holder.textViewPrice.setText(getItem(position).getPrice());
		holder.checkbox.setChecked(mSparseBooleanArray.get(position));
		holder.checkbox.setOnCheckedChangeListener(mCheckedChangeListener);

		imageLoader.displayImage(Constants.IMAGE_URL
				+ getItem(position).getIcon(), holder.imageView, options);

		return convertView;
	}

	OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub
			mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
		}
	};

	static class ViewHolder {

		TextView textViewTitle;

		TextView textViewDescription;

		TextView textViewPrice;

		ImageView imageView;

		CheckBox checkbox;
	}
}
